{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banner_Top_06.06.18.jpg?raw=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Counting Routes Problem\n",
    "## Let's count the number of routes from the bottom-left square to the top-right square\n",
    "### Solve this problem for both the 4x4 board and the 5x5 board"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<table><tr><td><img src='https://i.stack.imgur.com/YK08Gm.png?raw=true'></td><td><img src='https://i.stack.imgur.com/IfEhXm.png?raw=true'></td></tr></table>\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def routes(x,y):\n",
    "    if x == 1: \n",
    "        return 1\n",
    "    elif y == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        return routes(x-1,y)+routes(x,y-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3432"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "routes(8,8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 5, 15, 35, 70]\n",
      "[1, 4, 10, 20, 35]\n",
      "[1, 3, 6, 10, 15]\n",
      "[1, 2, 3, 4, 5]\n",
      "[1, 1, 1, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "for x in (5,4,3,2,1):\n",
    "    print([routes(x, y) for y in [1,2,3,4,5]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Let's look at the third row above, the row starting with [1,3,6,10,15].\n",
    "### What is the pattern?  Is there a simple function that generates these numbers?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 6, 10, 15, 21, 28, 36, 45, 55]\n"
     ]
    }
   ],
   "source": [
    "print([routes(x, 3) for x in [1,2,3,4,5,6,7,8,9,10]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script src=\"https://cdn.geogebra.org/apps/deployggb.js\"></script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<script src=\"https://cdn.geogebra.org/apps/deployggb.js\"></script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"ggb-point\"></div>\n",
       "<script>\n",
       "  var ggbApp = new GGBApplet({\n",
       "      \"height\": 400,\n",
       "      \"showToolBar\": true,\n",
       "      \"showMenuBar\": true,\n",
       "      \"showAlgebraInput\": true,\n",
       "      \"showResetIcon\": true,\n",
       "      \"enableLabelDrags\": true,\n",
       "      \"enableRightClick\": true,\n",
       "      \"enableShiftDragZoom\": true,\n",
       "      \"useBrowserForJS\": false,\n",
       "      \"filename\": \"files/GeogebraDemo.ggb\"\n",
       "  }, 'ggb-point');\n",
       "\n",
       "  ggbApp.inject();\n",
       "</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<div id=\"ggb-point\"></div>\n",
    "<script>\n",
    "  var ggbApp = new GGBApplet({\n",
    "      \"height\": 400,\n",
    "      \"showToolBar\": true,\n",
    "      \"showMenuBar\": true,\n",
    "      \"showAlgebraInput\": true,\n",
    "      \"showResetIcon\": true,\n",
    "      \"enableLabelDrags\": true,\n",
    "      \"enableRightClick\": true,\n",
    "      \"enableShiftDragZoom\": true,\n",
    "      \"useBrowserForJS\": false,\n",
    "      \"filename\": \"files/GeogebraDemo.ggb\"\n",
    "  }, 'ggb-point');\n",
    "\n",
    "  ggbApp.inject();\n",
    "</script>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Let f(1)=1, f(2)=3, f(3)=6, f(4)=10, f(5)=15.  What is the function f(x)?\n",
    "### In the above GeoGebra app, you found constants a, b, c for which f(x)=ax^2+bx+c.  Let's test it out!\n",
    "### Can you explain WHY this formula is true?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2.0999999999999996, 4.6, 8.299999999999999, 13.2, 19.3, 26.599999999999998, 35.1, 44.8]\n"
     ]
    }
   ],
   "source": [
    "def f(x):\n",
    "    # replace the numbers 0.6, 0.7, 0.8 with the numbers a, b, c you found above\n",
    "    return (0.6)*x*x + (0.7)* x + (0.8)\n",
    "\n",
    "print([f(1),f(2),f(3),f(4),f(5),f(6),f(7),f(8)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banners_Bottom_06.06.18.jpg?raw=true)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
